package ca.unbc.cpsc672;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import ca.unbc.cpsc672.fuzzy.FuzzySet;
import ca.unbc.cpsc672.fuzzy.IOUtils;

public class FuzzySetDialog extends Dialog {
	private Map<String, FuzzySet> setMap = new HashMap<>();

	public FuzzySetDialog(Shell parentShell) throws IOException {
		super(parentShell);
		this.setMap = IOUtils.loadFuzzySet();
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(GUIUtils.buildGridData());

		buildTabFolder(composite);
		return super.createContents(parent);
	}

	private void buildTabFolder(final Composite composite) {
		final CTabFolder cf = new CTabFolder(composite, SWT.NONE);
		cf.setLayoutData(GUIUtils.buildGridData());
		for (String name : setMap.keySet()) {
			CTabItem ti = new CTabItem(cf, SWT.NONE);
			ti.setText(name);
			FuzzySetTab tab = new FuzzySetTab(setMap.get(name));
			ti.setControl(tab.createContents(cf));
		}
		cf.setSelection(0);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Modify Fuzzy Sets");
	}

	@Override
	protected void okPressed() {
		try {
			IOUtils.saveFuzzySet(setMap.values());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.okPressed();
	}
}